﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace NotAClue
{
    public static class ArrayExtensionMethods
    {
        ///// <summary>
        ///// Returns a copy of the array of string 
        ///// all in lowercase
        ///// </summary>
        ///// <param name="strings">Array of strings</param>
        ///// <returns>array of string all in lowercase</returns>
        //public static String[] AllToLower(this String[] strings)
        //{
        //    String[] temp = new String[strings.Count()];
        //    for (int i = 0; i < strings.Count(); i++)
        //    {
        //        temp[i] = strings[i].ToLower();
        //    }
        //    return temp;
        //}

        ///// <summary>
        ///// Returns a copy of the array of string 
        ///// all in lowercase
        ///// </summary>
        ///// <param name="strings">Array of strings</param>
        ///// <returns>array of string all in lowercase</returns>
        //public static String[] AllToLower(this String[] strings)
        //{
        //    String[] temp = new String[strings.Count()];
        //    for (int i = 0; i < strings.Count(); i++)
        //    {
        //        temp[i] = strings[i].ToLower();
        //    }
        //    return temp;
        //}

        /// <summary>
        /// Determines whether the specified STR1 contains any.
        /// </summary>
        /// <param name="str1">The STR1.</param>
        /// <param name="str2">The STR2.</param>
        /// <returns>
        /// 	<c>true</c> if the specified STR1 contains any; otherwise, <c>false</c>.
        /// </returns>
        public static Boolean ContainsAny(this String[] str1, params String[] str2)
        {
            // call extension method to convert array to lower case for compare
            var lowerCaseStr1 = str1.AllToLower();
            foreach (String str in str2)
            {
                if (lowerCaseStr1.Contains(str.ToLower()))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Converts a CSV to a List of type int.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<int> ToIntList(this String list)
        {
            var invoices = new List<int>();

            // split string
            var csvList = list.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            // extract ints to of List<int>
            foreach (var invoiceId in csvList)
            {
                int value;
                if (int.TryParse(invoiceId, out value))
                    invoices.Add(value);
            }
            return invoices;
        }

        /// <summary>
        /// Toes the CSV string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static String ToCsvString<T>(this List<T> list)
        {
            var toString = new StringBuilder();
            foreach (var item in list)
            {
                toString.Append(item.ToString() + ",");
            }
            return toString.ToString().Substring(0, toString.Length - 1);
        }

        /// <summary>
        /// Toes the CSV string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static String ToCsvString<T>(this ReadOnlyCollection<T> list)
        {
            var toString = new StringBuilder();
            foreach (var item in list)
            {
                toString.Append(item.ToString() + ",");
            }
            return toString.ToString().Substring(0, toString.Length - 1);
        }

        /// <summary>
        /// Toes the CSV string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static String ToCsvString<T>(this T[] list)
        {
            var toString = new StringBuilder();
            foreach (var item in list)
            {
                toString.Append(item.ToString() + ",");
            }
            return toString.ToString().Substring(0, toString.Length - 1);
        }
    }
}
